﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using V2TEK.NET.SH.Silverlight.Charts.GanttHelper;
using V2TEK.NET.SH.Silverlight.Charts.Core.Schedule;
using System.Windows.Media.Imaging;
using System.Windows.Data;

namespace V2TEK.NET.SH.Silverlight.Charts.TreeGridHelper
{
    /// <summary>
    /// 该Expander可以根据不同的任务节点类型显示不同的图标
    /// </summary>
    public class SimpleTaskExpander : AbstractExpander
    {
        public static DependencyProperty TaskTypeProperty = DependencyProperty.Register("TaskType", typeof(TaskType), typeof(SimpleTaskExpander), new PropertyMetadata(TaskType.Task));

        public SimpleTaskExpander()
        {
            this.DefaultStyleKey = typeof(SimpleTaskExpander);
        }

        public TaskType TaskType
        {
            get
            {
                return (TaskType)GetValue(TaskTypeProperty);
            }
            set 
            {
                SetValue(TaskTypeProperty, value);
                //任务类型更改后，使用不同的图标
                //BindImage();
            }
        }

        /// <summary>
        /// string[0] = UnExpandedControl Uri string; string[1] = ExpandedControl Uri string
        /// </summary>
        public TaskIconDataContext ExpanderIconDescriptor
        {
            get;
            set;
        }

        protected Image ExpandedImage
        {
            get;
            set;
        }

        protected Image UnExpandedImage
        {
            get;
            set;
        }

        protected Button ExpanderBtn
        {
            get;
            set;
        }

        public Border Indention
        {
            get;
            set;
        }

        public Image Icon
        {
            get;
            set;
        }

        public TaskEntryLayoutItem DataItem
        {
            get
            {
                return DataContext as TaskEntryLayoutItem;
            }
        }

        public string BtnText
        {
            get
            {
                if (IsExpand)
                    return "-";
                else
                    return "+";
            }
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            this.ExpanderBtn = this.GetTemplateChild("ExpanderBtn") as Button;
            this.ExpanderBtn.Click += new RoutedEventHandler(ExpanderBtn_Click);
            this.Indention = this.GetTemplateChild("Indention") as Border;
            Update();
        }

        void ExpanderBtn_Click(object sender, RoutedEventArgs e)
        {
            ExpanderBtnClick();
        }

        protected virtual void ExpanderBtnClick()
        {
            RaiseExpandChange(!this.IsExpand);
            this.IsExpand = !this.IsExpand;
            //this.Update();
            this.InvalidateIcon();
        }

        //private void BindImage()
        //{
        //    Binding bind = new Binding("TaskType");
        //    bind.Source = DataContext;
        //    bind.Mode = BindingMode.OneWay;
        //    this.SetBinding(TaskTypeProperty, bind);

        //    IconRepresenter icons = ExpanderIconDescriptor.GetIconByTaskType(TaskType);
        //    this.UnExpandedImage = icons.ExpandedIcon as Image;
        //    this.ExpandedImage = icons.CollapseIcon as Image;
        //}

        protected virtual void InvalidateIndention()
        {
            if (this.Indention != null)
            {
                this.Indention.Width = new LevelToIndentionConverter().Convert(DataItem.Level);
            }
        }

        protected virtual void InvalidateIcon()
        {
            if (this.ExpanderBtn != null)
            {
                if (!DataItem.EnableExpand || DataItem.Data.Children.Count == 0)
                    this.ExpanderBtn.Visibility = Visibility.Collapsed;
                else
                {
                    this.ExpanderBtn.Content = this.BtnText;
                    this.ExpanderBtn.Visibility = Visibility.Visible;
                }
            }
        }

        public override void Update()
        {
            this.IsExpand = DataItem.IsExpand;
            InvalidateIndention();
            InvalidateIcon();
        }
    }
}
